home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.sprintlink.net!eskimo!scs
- From: scs@eskimo.com (Steve Summit)
- Subject: why arrays may seem like pointers (was: What is &Variable ... )
- X-Nntp-Posting-Host: eskimo.com
- Message-ID: <DnHyrp.CF8@eskimo.com>
- Sender: news@eskimo.com (News User Id)
- Organization: schmorganization
- References: <4gsdno$1bg@umbc9.umbc.edu> <4gtab6$acb@ceylon.gte.com> <313318b8.53776146@nntp.ix.netcom.com>
- Date: Wed, 28 Feb 1996 17:47:49 GMT
-
- In article <313318b8.53776146@nntp.ix.netcom.com>, miker3@ix.netcom.com
- (Mike Rubenstein) writes:
- > Brenda <g051286> wrote:
- >> schlein@umbc.edu (Jonas J. Schlein) wrote:
- >>> Yes it could and yes it is...'myarray' is not a pointer, but &myarray is
- >>> a pointer to 'myarray'.
- >>
- >> Um, that's not correct. myarray is DEFINITELY a pointer! ...
- >
- > NO. NO. NO. Where do people get this idea that arrays are pointers.
- > Arrays are arrays and pointers are pointers. In many, but not all,
- > situations an array is converted to a pointer.
-
- I know of at least two reasons why confusion here persists.
- The first, of course, is that as formal parameters to functions,
- what look like (that is, what may be declared as) arrays are
- really pointers. It's assumed that some programmers read more
- into this facet of C's pointer/array "equivalence" than is the
- case. (It's been suggested that the pagination between pages 99
- and 100 of K&R2 doesn't help any, although I sincerely hope that
- no one tries to learn C by opening K&R2 at random and reading the
- top of page 100 in isolation.)
-
- I recently discovered a second reason. I teach a small, informal
- class on C programming, and one of my main reasons for teaching
- it is so that *I* can learn where some of these perpetual
- misconceptions and difficulties come from. I've noticed that if
- I say, "In blah blah blah, an array is converted into a pointer,"
- some students hear that and think that the array itself has been
- converted into a pointer, now and forever. (Perhaps this notion
- is reinforced by the fact that, in a few other situations, such
- as function parameters, C does rewrite declarations for you.)
- In other words, when you say "When an array reference appears in
- an expression, it is converted into a pointer," what a student
- may hear and learn is something like "When you reference an array
- in an expression, the array is changed into a pointer." It's a
- small step from there to noticing that, since you can hardly use
- an array without referencing it in some expression somewhere in
- your program, by the time you're done, all your arrays will have
- been changed into pointers, and you won't have any arrays left.
-
- (In a similar vein, I've had a student ask whether code like
-
- int i = 1, j = 2;
- float f;
- f = (float)i / j;
-
- changed i into a float permanently. Would this make any sense at
- all? Not to those of us who know C already, but a student, by
- definition, doesn't, and can form the most marvelously peculiar
- models is left to his own devices.)
-
- Having learned this, I'm now careful to say things like, "When
- you make reference to an array in an expression, the compiler
- automatically generates a pointer to its first element, just as
- if you'd written &array[0]." I try to use words like "generate,"
- and *not* to use words like "convert."
-
- (As a final note: I composed this article to answer Mike's
- question, which is a good one, and not to poke any fun at him,
- but you might be intrigued if you go back, as I just did, and
- look at the words he happened to use.)
-
- Steve Summit
- scs@eskimo.com
- --
- The Communications Decency Act within the Telecommunications Act
- of 1996 (U.S.) is an annoying, threatening, abusive, indecent,
- and obscene piece of legislation which attempts to ban annoying,
- threatening, abusive, indecent, or obscene communication.
-